/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tigris.jasmincc.implementation;

import java.util.*;

import org.tigris.jasmincc.*;
import org.tigris.jasmincc.framework.*;
import org.tigris.mappedlexer.validator.VerifyHub;

/**
 * Syntax { {...} | ...() | <...> | "..." | ... }
 * 
 * 
 * @author Sam Huang
 * 
 */
public class MethodBodyProduction extends AbstractProduction {

	protected JavaCCModel javaccModel = null;

	protected NamingManager namingManager = null;

	protected Production regularExpressionProduction = null;

	protected Map<String, Object> cache = null;

	public void parse() throws Exception {
		namingManager = (NamingManager) cache.get("namingManager");
		tk = lexer.nexttok("{");
		tk = lexer.nexttok(false);
		while (!tk.equals("}")) {
			if (VerifyHub.IDENTIFIER.validate(tk)) {
				String nexttk = lexer.nexttok(false);
				if (nexttk.equals("(") && !tk.equals(JAVACC_LOOKAHEAD)) {
					tk = namingManager.updateRef(tk);
				}
				// if it is not the form of "<id> (" then simply print out
				// whatever
				// read
				lexer.print(tk).print(nexttk);
			} else if (tk.equals("{")) {
				// Java code, skip all until "}"
				tk = lexer.skip(true, '{');
			} else if (tk.equals("\"")) {
				tk = lexer.skipQutedString();
				lexer.print(tk).print(" ");
			} else if (tk.equals("<")) { // <[hello.]tag>
				tk = lexer.nexttok(false); // the name of the token
				if (!tk.equals("EOF")) {
					tk = namingManager.updateRef(tk);
				}
				tk = lexer.nexttok();
				if (tk.equals(":")) {
					tk = lexer.skipTo('>');
					lexer.print("<").print(tk).print(":").print(tk);
				} else {
					lexer.print("<").print(tk).print(tk);
				}
				// tk = lexer.nexttok(">"); // should be ">"
			} else if (tk.equals("#")) {
				// prefixRef(); ?
				javaccModel.setTree(true);
				lexer.print(tk);
				tk = lexer.nexttok(true);
			} else {
				// whatever, print!
				lexer.print(tk);
			}
			tk = lexer.nexttok(false);
		}
		lexer.print("}");
		lexer.print("\n");
	}

	public void setCache(Map<String, Object> cache) {
		this.cache = cache;
	}

	/**
	 * @param javaccModel
	 *            the javaccModel to set
	 */
	public void setJavaccModel(JavaCCModel javaccModel) {
		this.javaccModel = javaccModel;
	}

	/**
	 * @param regularExpressionProduction
	 *            the regularExpressionProduction to set
	 */
	public void setRegularExpressionProduction(
			Production regularExpressionProduction) {
		this.regularExpressionProduction = regularExpressionProduction;
	}

}
